//
//  AnimationCollectDatabase.m
//  Double
//
//  Created by dllo on 15/5/28.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "AnimationCollectDatabase.h"
#import <sqlite3.h>
#import "DB.h"
#import "AnimationCollectModel.h"

@implementation AnimationCollectDatabase

+ (void)commitSql:(NSString *)str
{
    sqlite3 * db = [DB openDB];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"数据库开启成功");
    } else {
        NSLog(@"数据库开启失败");
    }
}
// 插入
+ (void)insertWithModel:(AnimationCollectModel *)model
{
    NSString * str = [NSString stringWithFormat:@"insert into AnimationCollect(Id, Name, pic, uid, idmin) values (%ld, '%@', '%@', %ld, %ld)", model.AnimationId, model.AnimationName, model.AnimationPic, model.uid, model.idmin];
    [AnimationCollectDatabase commitSql:str];
}
// 删除
+ (void)deleteWithModel:(AnimationCollectModel *)model
{
    NSString * str = [NSString stringWithFormat:@"delete from AnimationCollect where idmin = %ld", model.idmin];
    [AnimationCollectDatabase commitSql:str];
}
// 更新
+ (void)updataWithModel:(NSInteger)Id Name:(NSString *)Name  pic:(NSString *)pic
{
    NSString * str = [NSString stringWithFormat:@"updata AnimationCollect set Id = %ld, Name = '%@', pic = '%@'", Id, Name, pic];
    [AnimationCollectDatabase commitSql:str];
}

//查询全部
+ (NSArray *)selectAll
{
    NSString * str = @"select Id, Name, pic, uid , idmin from AnimationCollect";
    sqlite3 * db = [DB openDB];
    // 定义执行结果接收者 + 限制长度 - 1 不限长度 &stmt查询结果所在位置
    sqlite3_stmt * stmt = nil;
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int Id = sqlite3_column_int(stmt, 0);
            const unsigned char * Name = sqlite3_column_text(stmt, 1);
            const unsigned char * pic = sqlite3_column_text(stmt, 2);
            int uid = sqlite3_column_int(stmt, 3);
            int idmin = sqlite3_column_int(stmt, 4);
            // 把C语言字符串转换成oc字符串
            NSString * AnimationName = [NSString stringWithUTF8String:(const char *)Name];
            NSString * Pic = [NSString stringWithUTF8String:(const char *)pic];
            AnimationCollectModel * model = [AnimationCollectModel AnimationCollectModelWithAnimationId:Id AnimationName:AnimationName AnimationPic:Pic uid:uid idmin:idmin];
            [array addObject:model];
        }
    }
    return array;
}

//根据ID  UID 模糊查询
+ (NSArray *)selectID:(NSInteger)idmin UID:(NSInteger)uid
{
    NSString * str = [NSString stringWithFormat:@"select Id, Name, pic, uid, idmin from AnimationCollect Where idmin = %ld AND uid = %ld", idmin, uid];
    sqlite3 * db = [DB openDB];
    // 定义执行结果接收者 + 限制长度 - 1 不限长度 &stmt查询结果所在位置
    sqlite3_stmt * stmt = nil;
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int Id = sqlite3_column_int(stmt, 0);
            const unsigned char * Name = sqlite3_column_text(stmt, 1);
            const unsigned char * pic = sqlite3_column_text(stmt, 2);
            int uid = sqlite3_column_int(stmt, 3);
            int idmin = sqlite3_column_int(stmt, 4);
            // 把C语言字符串转换成oc字符串
            NSString * AnimationName = [NSString stringWithUTF8String:(const char *)Name];
            NSString * Pic = [NSString stringWithUTF8String:(const char *)pic];
            AnimationCollectModel * model = [AnimationCollectModel AnimationCollectModelWithAnimationId:Id AnimationName:AnimationName AnimationPic:Pic uid:uid idmin:idmin];
            [array addObject:model];
        }
    }
    return array;
}
//根据ID 查询
+ (NSArray *)selectID:(NSInteger)Id
{
    NSString * str = [NSString stringWithFormat:@"select Id, Name, pic, uid, idmin from AnimationCollect Where Id LIKE '%%%ld%%'", Id];
    sqlite3 * db = [DB openDB];
    // 定义执行结果接收者 + 限制长度 - 1 不限长度 &stmt查询结果所在位置
    sqlite3_stmt * stmt = nil;
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int Id = sqlite3_column_int(stmt, 0);
            const unsigned char * Name = sqlite3_column_text(stmt, 1);
            const unsigned char * pic = sqlite3_column_text(stmt, 2);
            int uid = sqlite3_column_int(stmt, 3);
            int idmin = sqlite3_column_int(stmt, 4);
            // 把C语言字符串转换成oc字符串
            NSString * AnimationName = [NSString stringWithUTF8String:(const char *)Name];
            NSString * Pic = [NSString stringWithUTF8String:(const char *)pic];
            AnimationCollectModel * model = [AnimationCollectModel AnimationCollectModelWithAnimationId:Id AnimationName:AnimationName AnimationPic:Pic uid:uid idmin:idmin];
            [array addObject:model];
        }
    }
    return array;
}
//根据  UID 模糊查询
+ (NSArray *)selectUID:(NSInteger)uid
{
    NSString * str = [NSString stringWithFormat:@"select Id, Name, pic, uid, idmin from AnimationCollect Where uid = '%ld'", uid];
    sqlite3 * db = [DB openDB];
    // 定义执行结果接收者 + 限制长度 - 1 不限长度 &stmt查询结果所在位置
    sqlite3_stmt * stmt = nil;
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int Id = sqlite3_column_int(stmt, 0);
            const unsigned char * Name = sqlite3_column_text(stmt, 1);
            const unsigned char * pic = sqlite3_column_text(stmt, 2);
            int uid = sqlite3_column_int(stmt, 3);
            int idmin = sqlite3_column_int(stmt, 4);
            // 把C语言字符串转换成oc字符串
            NSString * AnimationName = [NSString stringWithUTF8String:(const char *)Name];
            NSString * Pic = [NSString stringWithUTF8String:(const char *)pic];
            AnimationCollectModel * model = [AnimationCollectModel AnimationCollectModelWithAnimationId:Id AnimationName:AnimationName AnimationPic:Pic uid:uid idmin:idmin];
            [array addObject:model];
        }
    }
    return array;
}





@end
